import Vue from 'vue'
import VueRouter from 'vue-router'
import GetRegister from '../views/GetRegister.vue'
import GetProject from '../views/GetProject.vue'
import GetMedicine from '../views/GetMedicine.vue'
import Arrange from '../views/Arrange.vue'
import Main from '../views/Main.vue'
import Login from '../views/Login.vue'
import RoutineBloodTest from '../views/RoutineBloodTest.vue'
import RoutineUrineTest from '../views/RoutineUrineTest.vue'
import Iconography from '../views/Iconography.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/queryMedicine',
        name: 'QueryMedicine',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/QueryMedicine.vue')
    }, {
        path: '/outMedicine',
        name: 'OutMedicine',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/OutMedicine.vue')
    },
    {
        path: '/bloodExamination',
        name: 'BloodExamination',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/BloodExamination.vue')
    }, {
        path: '/guahao',
        name: 'Guahao',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Guahao.vue')
    },
    {
        path: '/pay',
        name: 'Pay',
        component: () =>
            import ( /* webpackChunkName: "about" */ '../views/Pay.vue')
    },
    {
        path: '/getRegister',
        name: 'GetRegister',
        component: GetRegister
    },
    {
        path: '/getProject',
        name: 'GetProject',
        component: GetProject

    },
    {
        path: '/getMedicine',
        name: 'GetMedicine',
        component: GetMedicine
    },
    {
        path: '/arrange',
        name: 'Arrange',
        component: Arrange
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
    },
    {
        path: '/main',
        name: 'Main',
        component: Main
    },
    {
        path: '/routineBloodTest',
        name: 'RoutineBloodTest',
        component: RoutineBloodTest
    },
    {
        path: '/routineUrineTest',
        name: 'RoutineUrineTest',
        component: RoutineUrineTest
    },
    {
        path: '/iconography',
        name: 'Iconography',
        component: Iconography
    }
]


const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    // console.log(to)
    // console.log("↑为to，↓为from")
    // console.log(from)
    //1.如果登录和注册请求 放行
    //2.判断是否登录，如果已登录，则放行，否则跳转到登录页

    if (to.path == '/login' || to.path == '/main' || localStorage.getItem("position") == 'manager') {
        next()
    } else {
        if ((to.path == '/pay' || to.path == '/guahao') && localStorage.getItem("position") == 'chashier') {
            //挂号人员 放行缴费页面和挂号页面
            next();
        } else if ((to.path == '/queryMedicine' || to.path == '/outMedicine') && localStorage.getItem("position") == 'mediciner') {
            //药剂师 放行药品查询页面和处方出药页面
            next();
        } else if ((to.path == '/bloodExamination' || to.path == '/routineBloodTest' || to.path == '/routineUrineTest' || to.path == '/iconography') && localStorage.getItem("position") == 'checker') {
            //
            next();
        } else if ((to.path == '/getRegister' || to.path == '/getProject' || to.path == '/getMedicine') && localStorage.getItem("position") == 'odoctor') {
            next();
        } else if ((to.path == '/pay' || to.path == '/guahao' || to.path == '/queryMedicine' || to.path == '/outMedicine' || to.path == '/bloodExamination' ||
                to.path == '/outMedicine' || to.path == '/getRegister' || to.path == '/getProject' || to.path == '/getMedicine') && localStorage.getItem("position") == 'manager') {
            next();
        } else {
            if (from.path == '/login') {
                alert("请先登陆")
            } else if (localStorage.getItem("position") == null) {
                router.push('/login')
            } else {
                alert("没有权限")
            }
        }
    }

})

export default router